package com.lingshu.app.network;

import com.lingshu.app.config.DefaultConfig;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2024/1/8 10:11
 * @Description
 */
public class RetrofitAuthClient {
    private static volatile RetrofitAuthClient instance;
    private static Retrofit retrofit;
    private RetrofitAuthClient() {
    }

    public static RetrofitAuthClient getInstance() {
        if (instance == null) {
            synchronized (RetrofitAuthClient.class) {
                if (instance == null) {
                    instance = new RetrofitAuthClient();
                }
            }
        }
        return instance;
    }

    public <T> T getService(Class<T> cls) {
        return getRetrofit().create(cls);
    }

    private synchronized Retrofit getRetrofit() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder().baseUrl(DefaultConfig.getInstance().getAuthHost())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
